/**
 * Created by Administrator on 2016/10/16.
 */
var express = require("express");
var router = express.Router();
var mysql = require("mysql");
var DateUtil = require('../../lib/utils/DateUtil');
var _ = require("underscore");
var moment = require('moment');

var connection = require('../../lib/utils/DbconnectUtil');

/**
 * 查询捐增列表
 */
router.get('/', function (req, res, next) {
    var sqlStr = "select * from t_order where status = 1 order by start_time desc ";
    connection().query(sqlStr, function (err, results) {
        var resultData = {};
        if (err) {
            console.log(err);
            resultData = {code : -1, data : 0, message:"query exception"};

        } else {
            _.forEach(results,function (result) {
                result.total_fee = parseFloat(result.total_fee)/100;
                result.start_time = result.start_time && moment(result.start_time).format('YYYY-MM-DD HH:mm:ss');
                result.end_time = result.end_time && moment(result.end_time).format('YYYY-MM-DD HH:mm:ss')
            })

            resultData = {code : 0, data : results};
        }
        res.send(resultData);
    });
});

/**
 * 查询项目内容
 */
router.get('/:orderId', function (req, res, next) {
    var sqlStr = "select * from t_order where status = 1 and id = ? ";
    connection().query(sqlStr, [req.params.orderId], function (err, results) {
        var resultData = {};
        if (error || results.length == 0) {
            console.log(error);
            res.send({code: -1, message: '查询数据失败'});
        } else {
            resultData = results[0];
            resultData.total_fee = parseFloat(result.total_fee)/100;
            resultData.start_time = moment(result.start_time).format('YYYY-MM-DD HH:mm:ss');
            resultData.end_time = moment(result.end_time).format('YYYY-MM-DD HH:mm:ss')
        }
        res.send({code : 0, data : resultData});
    });
});


/**
 * 更新内容
 */
router.post('/', function (req, res, next) {
    var order = req.body;
    order.id = new Date().getTime()+ '';
    order.startTime = order.startTime ? new Date(order.startTime) : new Date();
    if (!_.isDate(order.startTime)) {
        console.log('startTime is not date :' + order.startTime);
    }
    order.endTime = order.endTime ? new Date(order.endTime) : new Date();
    if (!_.isDate(order.endTime)) {
        console.log('endTime is not date :' + order.endTime);
    }

    order.totalFee = parseFloat(order.totalFee)*100;
    order.userType = 0;

    connection().query('INSERT INTO  t_order SET id = ?, name = ?, phone_number = ?, total_fee = ?, user_type = ?,company = ?, project_name = ?, start_time = ?, end_time = ?, status = 1 ',
        [ order.id, order.name, order.phoneNumber, order.totalFee, order.userType, order.company, order.projectName, order.startTime, order.endTime], function (err, results) {
            if (err) {
                console.log(err);
                res.send({code: -1, message: '保存数据失败'});
            } else {
                res.send({code: 0, data: order});
            }
        });
});

/**
 * 删除项目内容
 */
router.delete('/:orderId', function (req, res, next) {
    connection().query('DELETE FROM  t_order WHERE id = ?', [req.params.orderId], function (err, results) {
        if (err) {
            console.log(err);
            res.send({code: -1, message: '删除数据失败'});
        } else {
            res.send({code: 0, data: results});
        }
    });
});

module.exports = router;